<<<<<<< HEAD
package com.hsx.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

public class FileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取下载路径
//        D:\Java\JavaWeb\response\src\main\resources\idea.PNG
//        idea.PNG
        String realPath ="D:\\Java\\JavaWeb\\response\\src\\main\\resources\\idea.PNG";
        System.out.println("真实路径"+realPath);
        // 2.下载文件名是啥
        String filename = realPath.substring(realPath.lastIndexOf("\\")+1);
        // 3.设置办法让浏览器支持Content-Disposition我们需要的东西
        resp.setHeader("Content-Disposition","attachment;filename="+filename);
        // 4.获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
        // 5.创建缓冲区
        int len=0;
        byte[] buffer = new byte[1024];
        // 6.获取OutputStream对象
        ServletOutputStream out = resp.getOutputStream();
        // 7.将FileOutputStream写入buffer缓冲区,
        // 使用OutputStream将buff缓冲区的数据输出的客户端
        while((len=in.read(buffer))>0){
            out.write(buffer,0,len);
        }
        in.close();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
=======
package com.hsx.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

public class FileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取下载路径
//        D:\Java\JavaWeb\response\src\main\resources\idea.PNG
//        idea.PNG
        String realPath ="D:\\Java\\JavaWeb\\response\\src\\main\\resources\\idea.PNG";
        System.out.println("真实路径"+realPath);
        // 2.下载文件名是啥
        String filename = realPath.substring(realPath.lastIndexOf("\\")+1);
        // 3.设置办法让浏览器支持Content-Disposition我们需要的东西
        resp.setHeader("Content-Disposition","attachment;filename="+filename);
        // 4.获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
        // 5.创建缓冲区
        int len=0;
        byte[] buffer = new byte[1024];
        // 6.获取OutputStream对象
        ServletOutputStream out = resp.getOutputStream();
        // 7.将FileOutputStream写入buffer缓冲区,
        // 使用OutputStream将buff缓冲区的数据输出的客户端
        while((len=in.read(buffer))>0){
            out.write(buffer,0,len);
        }
        in.close();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
>>>>>>> 0955e43271ec003176a75da3f24c6d9c0071fe9b
